//const baseUrl = 'http://10.0.140.123:9080';
const baseUrl = 'http://huichat.yonghui.cn:10080';

export default (Vue)=>{
	
	Vue.prototype.http = {
		baseUrl:baseUrl,
		post: (url,data,callback)=>{
			Vue.$vux.loading.show({ text: '请求中...' });
			let loginName = JSON.parse(sessionStorage.getItem("userinfo")).loginName;
			url += /\?/.test(url)?`&ekp=${loginName}`:`?ekp=${loginName}`;
			if(!data.data.hasOwnProperty('s_fromApp')){data.data.s_fromApp = 'kk';}
			if(!/http/.test(url)){url = baseUrl+url};
			Vue.http.post(
				url,
				data.data,
				{headers:{
					'Content-Type':'application/json',
					'X-Requested-With':'XMLHttpRequest'
				}}
			).then((res)=>{
				Vue.$vux.loading.hide();
				callback(res.body);
			},(err)=>{
				Vue.$vux.loading.hide();
				Vue.$vux.loading.show({ text: '请求失败！' });
				setTimeout(()=>{
					Vue.$vux.loading.hide();
				},2000);
			})
		},
		fdpost: (url,params,callback)=>{
			Vue.$vux.loading.show({ text: '请求中...' });
			let loginName = JSON.parse(sessionStorage.getItem("userinfo")).loginName;
			url += /\?/.test(url)?`&ekp=${loginName}`:`?ekp=${loginName}`;
			if(!params.data.hasOwnProperty('s_fromApp')){params.data.s_fromApp = 'kk';}
			Vue.http.post(
				baseUrl+url,
				params.data,
				{headers:{
					'Content-Type':'application/json',
					'X-Requested-With':'XMLHttpRequest'
				}}
			).then((res)=>{
				Vue.$vux.loading.hide();
				callback(res.body);
			},(err)=>{
				Vue.$vux.loading.hide();
				Vue.$vux.loading.show({ text: '请求失败！' });
				setTimeout(()=>{
					Vue.$vux.loading.hide();
				},2000);
			})
		},
		
		getBudget(options,callback){
			const {type='1',params,money} = options;
			const httpMap = { 
				'1':{ func:getDepartBudget, target:'fdCostCenterCode', },
				'2':{ func:getDepartBudget, target:'fdFactCostCode', },
				'3':{ func:getSpecialBudget, target:'fdSpecialCode', },
			};
			if(options.status=='10'){return callback({status:true});}
			httpMap[type] && httpMap[type].func.call(this);
			function getDepartBudget(){
				this.fdpost("/fk/mobile/fkMobileBase.do?method=getDepartBudget",{
					data: {
						'fdYear': new Date().getFullYear(),	
						'fdCostCenterCode': params[httpMap[type].target],
						'fdAccountCode': '6600410100'//写死的
					},
				},(redata)=>{
					if(parseFloat(money)>parseFloat(redata.data[0].fdFundCanUseMoney)){
						return callback({status:false,msg:'已超出基金中心预算可使用总金额'});
					}else if(money == 0){
						return callback({status:false,msg:'报销金额不能为0'});
					}else{
						return callback({status:true});
					}
				})
			}
			function getSpecialBudget(){
				this.fdpost("/fk/mobile/fkMobileBase.do?method=getSpecialBudget",{
					data: {
						'fdSpecialCode': params[httpMap[type].target],
					},
				},(redata)=>{
					if(parseFloat(money)>parseFloat(redata.data[0].fdSpecialCanUseMoney)){
						return callback({status:false,msg:'已超出专项可使用金额'});
					}else if(money == 0){
						return callback({status:false,msg:'报销金额不能为0'});
					}else{
						return callback({status:true});
					}
				})
			}
		},
		
	};
	
};